package com.aegis.demo;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import cn.hutool.json.JSONObject;
import org.junit.Test;

/**
 * @Author wuweixin
 *
 * @Version 1.0
 * @Descritube 当前demo演示了用户权限使用场景
 */
public class DemoMain1 {

    /**
     *
     */
    public static final String APP_ID = "71rv7uk9bdgtbnwop80sh7wxato";

    /**
     * 一定要保证这个接口地址是网关的，并且是被网关负载的
     */
    public static final String BASE_URL = "http://localhost:8700/demo1";

    private static final String HEAD_APP_ID = "X-AE-App-ID";

    public static final String HEAD_TOKEN = "Authorization";


    /**
     * 超管用户模拟调用登录接口、查询当前用户信息、查询某个用户的信息
     */
    @Test
    public void admin() {
        HttpRequest loginRequest = HttpUtil
                .createGet(BASE_URL + "/auth/login?roles=admin")
                .header(HEAD_APP_ID, APP_ID);
        String body = loginRequest.execute().body();
        System.out.println(body);
        JSONObject res = new JSONObject(body);
        String token = res.getJSONObject("tokenInfo").getStr("tokenValue");

        //查询个人资料
        HttpRequest queryCurrentUserInfoReq = HttpUtil
                .createGet(BASE_URL + "/auth/user/me/profile")
                .header(HEAD_TOKEN, token);
        String userInfoResBody1 = queryCurrentUserInfoReq.execute().body();
        System.out.println(userInfoResBody1);


        //查询某个用户的资料
        HttpRequest queryUserInfoReq = HttpUtil
                .createGet(BASE_URL + "/auth/user/123/profile")
                .header(HEAD_TOKEN, token);
        String userInfoResBody2 = queryUserInfoReq.execute().body();
        System.out.println(userInfoResBody2);
    }

    /**
     * 普通用户
     */
    @Test
    public void user() {
        HttpRequest loginRequest = HttpUtil
                .createGet(BASE_URL + "/auth/login?roles=user")
                .header(HEAD_APP_ID, APP_ID);
        String body = loginRequest.execute().body();
        System.out.println(body);
        JSONObject res = new JSONObject(body);
        String token = res.getJSONObject("tokenInfo").getStr("tokenValue");

        //查询个人资料
        HttpRequest queryCurrentUserInfoReq = HttpUtil
                .createGet(BASE_URL + "/auth/user/me/profile")
                .header(HEAD_TOKEN, token);
        String queryCurrentUserInfoReqBody = queryCurrentUserInfoReq.execute().body();
        System.out.println("查询个人资料:" + queryCurrentUserInfoReqBody);

        //查询个人资料-请求方法无权限（接口限定了只能GET请求，其余请求均无权限）
        HttpRequest queryCurrentUserInfoReq1 = HttpUtil
                .createPost(BASE_URL + "/auth/user/me/profile")
                .header(HEAD_TOKEN, token);
        String queryCurrentUserInfoReqBody1 = queryCurrentUserInfoReq1.execute().body();
        System.out.println("查询个人资料-请求的方法无权限:" + queryCurrentUserInfoReqBody1);


        //查询某个用户的资料-提示无权限
        HttpRequest queryUserInfoReq = HttpUtil
                .createGet(BASE_URL + "/auth/user/123/profile")
                .header(HEAD_TOKEN, token);
        String userInfoResBody2 = queryUserInfoReq.execute().body();
        System.out.println("查询某个用户的资料-无权限方法:" + userInfoResBody2);
    }

    /**
     * 普通管理员
     */
    @Test
    public void manage() {
        HttpRequest loginRequest = HttpUtil
                .createGet(BASE_URL + "/auth/login?roles=manage")
                .header(HEAD_APP_ID, APP_ID);
        String body = loginRequest.execute().body();
        System.out.println(body);
        JSONObject res = new JSONObject(body);
        String token = res.getJSONObject("tokenInfo").getStr("tokenValue");

        //查询个人资料
        HttpRequest queryCurrentUserInfoReq = HttpUtil
                .createGet(BASE_URL + "/auth/user/me/profile")
                .header(HEAD_TOKEN, token);
        String queryCurrentUserInfoReqBody = queryCurrentUserInfoReq.execute().body();
        System.out.println("查询个人资料:" + queryCurrentUserInfoReqBody);


        String userProfileUrl = BASE_URL + "/auth/user/123/profile";

        //查询某个用户的资料
        HttpRequest queryUserInfoReq = HttpUtil
                .createGet(userProfileUrl)
                .header(HEAD_TOKEN, token);
        String userInfoResBody2 = queryUserInfoReq.execute().body();
        System.out.println("查询某个用户的资料:" + userInfoResBody2);

        //删除某个用户的资料-无权限提示
        HttpRequest deleteUserProfile = HttpUtil
                .createRequest(Method.DELETE, userProfileUrl)
                .header(HEAD_TOKEN, token);
        String deleteUserProfileBody = deleteUserProfile.execute().body();
        System.out.println("删除某个用户的资料-无权限方法:" + deleteUserProfileBody);
    }


}
